package com.lianxian.xindong.home.mvp.ui.holder;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.library.YLCircleImageView;
import com.bumptech.glide.Glide;
import com.hxb.library.base.BaseHolder;
import com.hxb.library.di.component.AppComponent;
import com.hxb.library.http.imageloader.ImageLoader;
import com.hxb.library.utils.HxbUtils;
import com.lianxian.xindong.commonsdk.BuildConfig;
import com.lianxian.xindong.commonsdk.imgaEngine.config.CommonImageConfigImpl;
import com.lianxian.xindong.home.R;
import com.lianxian.xindong.home.R2;
import com.lianxian.xindong.home.mvp.ui.weight.douyin.MyVideoPlayer;

import butterknife.BindView;

/**
 * @author: HeXingBo
 * @date: 2022/11/15
 * @description：
 */
public class VideoPlayerItemHolder extends BaseHolder<String> {

    @BindView(R2.id.mp_video)
    MyVideoPlayer mpVideo;
    @BindView(R2.id.tv_des)
    TextView tvDes;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.iv_head)
    YLCircleImageView ivHead;
    @BindView(R2.id.iv_care)
    ImageView ivCare;
    @BindView(R2.id.tv_zan)
    TextView tvZan;
    @BindView(R2.id.tv_msg_drop)
    TextView tvMsgDrop;
    @BindView(R2.id.iv_msg_hi)
    ImageView ivMsgHi;
    @BindView(R2.id.iv_liwu)
    ImageView ivLiwu;
    @BindView(R2.id.lv_right)
    LinearLayout lvRight;

    private AppComponent mAppComponent;
    private ImageLoader mImageLoader;//用于加载图片的管理类,默认使用 Glide,使用策略模式,可替换框架

    public VideoPlayerItemHolder(View itemView) {
        super(itemView);
        //可以在任何可以拿到 Context 的地方,拿到 AppComponent,从而得到用 Dagger 管理的单例对象
        mAppComponent = HxbUtils.obtainAppComponentFromContext(itemView.getContext());
        mImageLoader = mAppComponent.imageLoader();
    }

    @Override
    public void setData(@NonNull String data, int position) {
        ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        if (position == 0) {
            mpVideo.startVideo();
        }
        tvTitle.setText("第" + position + "个视频");
        //itemView 的 Context 就是 Activity, Glide 会自动处理并和该 Activity 的生命周期绑定
        mImageLoader.loadImage(itemView.getContext(),
                CommonImageConfigImpl.builder()
                        .url(TextUtils.isEmpty(data) ? BuildConfig.DEFAULT_IMG_URL : data)
                        .errorPic(R.drawable.ic_default_image)
                        .placeholder(R.drawable.ic_default_image)
                        .imageRadius(10)
                        .imageView(mpVideo.thumbImageView)
                        .build());
    }

    @Override
    protected void onRelease() {
        mImageLoader.clear(mAppComponent.application(), CommonImageConfigImpl.builder()
                .imageViews(mpVideo.thumbImageView)
                .build());
    }
}